#include "PCH.h"
#include "DeRenderTargets.h"
#include "RenderDevice.h"
using namespace DeviousRenderer;

DeRenderTargets::DeRenderTargets()
{
	for (int i = 0; i < GB_NUM; i ++)
	{
		m_GBufferRTVs[i] = 0;
		m_GBufferTexs[i] = 0;
	}
}

DeRenderTargets::~DeRenderTargets()
{
	ReleaseBuffers();
}

void DeRenderTargets::InitBuffers()
{

	DXGI_SAMPLE_DESC sampleDesc;
	sampleDesc.Count =1;
	sampleDesc.Quality = 0;

	Texture2DConfig RTConfig;
	RTConfig.SetColorBuffer(DeRenderDevice::Get()->GetResolutionX(), DeRenderDevice::Get()->GetResolutionY());
	RTConfig.SetFormat(DXGI_FORMAT_R32G32B32A32_FLOAT);
	RTConfig.SetBindFlags(D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
	RTConfig.SetSampleDesc(sampleDesc);
	for (int i = 0; i < GB_NUM; i ++)
	{
		m_GBufferTexs[i] = GRenderDevice->CreateTexture2D(&RTConfig);
		m_GBufferRTVs[i] = GRenderDevice->CreateRenderTargetView(m_GBufferTexs[i]);
	}

	Texture2DConfig	DTConfig;
	DTConfig.SetDepthBuffer(DeRenderDevice::Get()->GetResolutionX(), DeRenderDevice::Get()->GetResolutionY());
	DTConfig.SetFormat(DXGI_FORMAT_R24G8_TYPELESS);
	DTConfig.SetBindFlags(D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE);
	DTConfig.SetSampleDesc(sampleDesc);

	m_DepthTexture = GRenderDevice->CreateTexture2D(&DTConfig);
	m_DepthStencilView = GRenderDevice->CreateDepthStencilView(m_DepthTexture);

// 	DepthStencilViewConfig DSVConfig;
// 	D3D11_TEX2D_DSV DSVTexture2D;
// 	DSVTexture2D.MipSlice = 0;
// 	DSVConfig.SetTexture2D(DSVTexture2D);
// 	DSVConfig.SetFormat(DXGI_FORMAT_R24_UNORM_X8_TYPELESS);
}

void DeRenderTargets::ReleaseBuffers()
{
	SAFE_DELETE_ARRAY(m_GBufferTexs);
	SAFE_DELETE_ARRAY(m_GBufferRTVs);
}